package com.study.patterns.proxy.dynamic.asm.base;


import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

/**
 * Created by tan on 2017/6/22.
 */
//生成具体的构造方法字节码
public class InitMethodAdapter extends MethodVisitor {
	public InitMethodAdapter(int api, MethodVisitor mv) {
		super(api, mv);
	}

	@Override
	public void visitCode() {
		mv.visitVarInsn(Opcodes.ALOAD, 0);
		//调用父类构造方法
		mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
		mv.visitVarInsn(Opcodes.ALOAD, 0);
		//将常量池中的字符串常量加载到栈顶
		mv.visitLdcInsn("tanhaiyuan");
		//设置name属性值
		mv.visitFieldInsn(Opcodes.PUTFIELD, "com/study/patterns/proxy/dynamic/asm/base/MyClass", "name", Type.getDescriptor(String.class));
		//设置返回值
		mv.visitInsn(Opcodes.RETURN);
		//设置方法栈和本地变量表的大小
		mv.visitMaxs(2, 1);
	}
}
